package misc;

import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: nuno-rafael-figueiredo
 * Date: 20-02-2014
 * Time: 17:27
 */
public class Files {
    /*
             * Get the extension of a file.
             */
    public static Extension getExtension(File file) {
        Extension ext = null;
        String name = file.getName();
        int i = name.lastIndexOf('.');

        if (i > 0 && i < name.length() - 1)
            ext = new Extension(name.substring(i + 1));

        return ext;
    }

    public static class Extension{

        private final String extension;

        public Extension(String extension) {
            this.extension = extension.toLowerCase();
        }

        @Override
        public String toString() {
            return extension;
        }

        @Override
        public boolean equals(Object obj) {
            return super.equals(obj) || (obj instanceof Extension && ((Extension)obj).extension.equals(extension));
        }
    }
}
